package DivideAndConquer;

import java.util.Arrays;
import java.util.PriorityQueue;

public class _215_KthLargestElementinanArray {
    //Normal Solution:use sort
    public int findKthLargest_1(int[] nums, int k) {
        Arrays.sort(nums);
        return nums[nums.length - k];
    }

    //Solution 2:use priority queue
    public int findKthLargest(int[] nums, int k) {
        PriorityQueue<Integer> queue = new PriorityQueue<>();
        for (int num : nums) {
            queue.add(num);
            if (queue.size() > k) {
                queue.remove();
            }
        }
        return queue.peek();
    }

}
